<template>
  <div>
    <el-row>
      <el-col :span="12">
        <el-card>
          <el-form :inline="true">
            <el-form-item>
              <el-upload
                ref="upload"
                class="upload-demo"
                :on-success="submitSuccess"
                :on-error="submitError"
                :action="uploadBaseUrl+'&type='+radio"
                :on-preview="handlePreview"
                :on-remove="handleRemove"
                :file-list="fileList"
                :auto-upload="false"
              >
                <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
                <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">点击上传</el-button>
              </el-upload>
            </el-form-item>
            <el-form-item label="选择上传路径">
              <el-radio-group v-model="radio">
                <el-radio :label="1">前端</el-radio>
                <el-radio :label="0">后端</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-form>
          <el-divider content-position="left">前端</el-divider>
          <el-table
            height="550"
            :data="tomcatFile.slice((currentPage-1)*pageSize,currentPage*pageSize)"
          >
            <el-table-column
              show-overflow-tooltip
              label="文件名"
              align="center"
              prop="fileName"
            >
              <template slot-scope="scope">
                <span v-if="scope.row.status===0">{{ scope.row.fileName }}</span>
                <el-button v-else type="text" @click="readFileContent(1,scope.row.fileName)">{{ scope.row.fileName }}</el-button>
              </template>
            </el-table-column>
            <el-table-column
              show-overflow-tooltip
              label="文件大小"
              align="center"
              prop="size"
            ></el-table-column>
            <el-table-column
              show-overflow-tooltip
              label="创建时间"
              align="center"
              prop="createTime"
            ></el-table-column>
            <el-table-column
              show-overflow-tooltip
              label="文件类型"
              align="center"
              prop="fileType"
            ></el-table-column>
            <el-table-column
              show-overflow-tooltip
              label="文件后缀"
              align="center"
              prop="extension"
            ></el-table-column>
            <el-table-column
              label="操作"
              align="center"
            >
              <template slot-scope="scope">
                <el-button type="text" @click="reNameFile(1,scope.row)">重命名</el-button>
                <el-tooltip class="item" effect="dark" content="文件删除" placement="top-start">
                &nbsp;
                  <el-button
                    v-if="scope.row.fileType=='文件'"
                    type="text"
                    size="mini"
                    @click="removeFile(1,scope.row.fileName)"
                  >
                    删除
                  </el-button>
                </el-tooltip>
                <el-tooltip class="item" effect="dark" content="文件解压" placement="top-start">
                  <el-button v-if="scope.row.extension==='zip'" type="text" @click="unZipFile(1,scope.row.fileName)">解压</el-button>
                </el-tooltip>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            :current-page="currentPage"
            :page-size="pageSize"
            layout="prev, pager, next,total"
            :total="tomcatFile.length"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          >
          </el-pagination>
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card>
          <el-form :inline="true">
            <el-form-item>
              <el-tooltip class="item" effect="dark" content="点击可进行后端服务的重启" placement="top-start">
                <el-button type="primary" style="float: right" @click="reload()">一键重启</el-button>
              </el-tooltip>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="init">刷新</el-button>
            </el-form-item>
            <el-form-item>
              <el-alert
                :closable="false"
                :title="'最近一次重启时间> '+projectStartTime"
                type="success">
              </el-alert>
            </el-form-item>
          </el-form>
          <el-divider content-position="left">后端</el-divider>
          <el-table
            height="550"
            :data="projectFile.slice((currentPageBack-1)*pageSizeBack,currentPageBack*pageSizeBack)"
          >
            <el-table-column
              show-overflow-tooltip
              label="文件名"
              align="center"
              prop="fileName"
            >
              <template slot-scope="scope">
                <span v-if="scope.row.status===0">{{ scope.row.fileName }}</span>
                <el-button v-else type="text" @click="readFileContent(0,scope.row.fileName)">{{ scope.row.fileName }}</el-button>
              </template>
            </el-table-column>
            <el-table-column
              show-overflow-tooltip
              label="文件大小"
              align="center"
              prop="size"
            ></el-table-column>
            <el-table-column
              show-overflow-tooltip
              label="创建时间"
              align="center"
              prop="createTime"
            ></el-table-column>
            <el-table-column
              show-overflow-tooltip
              label="文件类型"
              align="center"
              prop="fileType"
            ></el-table-column>
            <el-table-column
              show-overflow-tooltip
              label="文件后缀"
              align="center"
              prop="extension"
            ></el-table-column>
            <el-table-column
              label="操作"
              align="center"
            >
              <template slot-scope="scope">
                <el-button type="text" @click="reNameFile(0,scope.row)">重命名</el-button>
                <el-tooltip class="item" effect="dark" content="文件删除" placement="top-start">
                  <el-button
                    v-if="scope.row.fileType=='文件'"
                    type="text"
                    size="mini"
                    @click="removeFile(0,scope.row.fileName)"
                  >
                    删除
                  </el-button>
                </el-tooltip>
                <el-tooltip class="item" effect="dark" content="文件解压" placement="top-start">
                  <el-button v-if="scope.row.extension==='zip'" type="text" @click="unZipFile(0,scope.row.fileName)">解压</el-button>
                </el-tooltip>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            :current-page="currentPageBack"
            :page-size="pageSizeBack"
            layout="prev, pager, next,total"
            :total="projectFile.length"
            @size-change="handleSizeChangeBack"
            @current-change="handleCurrentChangeBack"
          >
          </el-pagination>
        </el-card>
      </el-col>
    </el-row>

    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="50%"
      :before-close="handleClose"
    >
      <el-input v-model="fileContent" resize="none" readonly type="textarea" :rows="10"></el-input>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog
      :before-close="closeDialog"
      title="重命名"
      :visible.sync="reNameDialogVisible"
      width="30%"
    >
      <el-form>
        <el-form-item label="文件名">
          <span>{{ fileName }}</span>
        </el-form-item>
        <el-form-item label="新文件名">
          <el-input v-model="newFileName" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="updateFileName">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getFileList, getFileContent, removeFile, executeCommandAsync, reNameFileName, unZip, getEarlyStartTime } from '@/api/deploy'
import { getTokenKey, getToken } from '@/utils/token'
export default {
  name: 'DeployIndex',
  data() {
    return {
      reNameDialogVisible: false,
      uploadBaseUrl: window.SITE_CONFIG.deployUrl + '/deploy/uploadFile?' + getTokenKey() + '=' + getToken(),
      // uploadBaseUrl2: window.SITE_CONFIG.deployUrl + '/deploy/uploadFile?type=1&' + getTokenKey() + '=' + getToken(),
      fileList: [],
      fileContent: '',
      dialogVisible: false,
      projectFile: [],
      tomcatFile: [],
      currentPage: 1,
      currentPageBack: 1,
      pageSize: 6,
      pageSizeBack: 6,
      fileName: '',
      newFileName: '',
      type: '',
      radio: 1,
      projectStartTime: ''
    }
  },
  created() {
    this.init()
    this.getEarlyStartTime()
  },
  methods: {
    getEarlyStartTime() {
      getEarlyStartTime().then((r) => {
        this.projectStartTime = r
      })
    },
    unZipFile(status, fileName) {
      unZip({
        type: status,
        fileName: fileName
      }).then(() => {
        this.init()
        this.$message.success('解压成功')
      })
    },
    updateFileName() {
      if (this.newFileName === '') {
        this.$message.error('新文件名不能为空')
        return
      }
      reNameFileName({
        type: this.type,
        newFileName: this.newFileName,
        fileName: this.fileName
      }).then(() => {
        this.init()
        this.$message.success('操作成功')
        this.closeDialog()
      })
    },
    closeDialog() {
      this.type = this.fileName = this.newFileName = ''
      this.reNameDialogVisible = false
    },
    reNameFile(status, row) {
      this.reNameDialogVisible = true
      this.fileName = row.fileName
      this.type = status
    },
    submitSuccess(response, file, fileList) {
      if (response.code === 200) {
        this.$message.success('上传成功')
        this.init()
      } else {
        this.$message.error(response.msg)
        this.init()
      }
    },
    submitError(err, file, fileList) {
      console.log(err)
      this.$message.error('文件上传失败')
      this.init()
    },
    submitUpload() {
      this.$refs.upload.submit()
    },
    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    handlePreview(file) {
      console.log(file)
    },
    handleClose() {
      this.dialogVisible = false
    },
    readFileContent(status, fileName) {
      this.dialogVisible = true
      getFileContent(
        {
          type: status,
          fileName: fileName
        }
      ).then((r) => {
        this.fileContent = r
      })
    },
    // 文件删除
    removeFile(status, fileName) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        removeFile(
          {
            type: status,
            fileName: fileName
          }
        ).then(() => {
          this.init()
        })
      })
    },
    // 每页多少条
    handleSizeChange(val) {
      this.pageSize = val
    },
    // 当前页
    handleCurrentChange(val) {
      this.currentPage = val
    },
    // 每页多少条
    handleSizeChangeBack(val) {
      this.pageSizeBack = val
    },
    // 当前页
    handleCurrentChangeBack(val) {
      this.currentPageBack = val
    },
    reload() {
      this.$confirm('确定重启吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        executeCommandAsync({
          'cmd': './restart.sh'
        }).then(() => {
          this.$message.warning('重启命令已下发，请稍后')
        })
      })
    },
    init() {
      getFileList().then((r) => {
        this.tomcatFile = r.tomcatFile
        this.projectFile = r.projectFile
      })
    }
  }
}
</script>

<style scoped>
.table-style{
  height: 400px;
  overflow: auto;
}
</style>
